<template>
    <div class="cmt-comtent">
        <h3>发表评论</h3>
        <hr>
        <textarea placeholder="请输入要评论的内容（最多120个字）" maxlength="120" v-model="name"></textarea>
        <mt-button type="primary" size="large" @click="postComment1">发表评论</mt-button>
        <div class="cmt-list">
            <div class="cmt-item" v-for="(val,i) in comments" :key="i">
                <div class="cmt-title">
                    第{{i+1}}楼&nbsp;&nbsp;:用户：{{val.user_name}}&nbsp;&nbsp;发表时间：{{val.add_time|dtaFormat}}
                </div>
                <div class="cmt-body">
                    {{val.content==='undefined'?'此用户暂未评论':val.content}}
                </div>
            </div>
        </div>
        <mt-button type="danger" size="large" plain @click="getMore1">加载更多</mt-button>
    </div>
</template>
<script>
import axios from 'axios'
// import vue from 'vue'
// import Resource from 'vue-resource'
// vue.use(Resource)
import {Toast} from 'mint-ui'
import qs from 'qs';

    export default {
        data(){
            return {
                pageIndex:1,
                comments:[],
                name:'',
                cmt:{}
                // Id:this.$route.params.id
                
            }
        },
        created() {
            this.getcmt1()
        },
        methods:{
            
            getcmt1(){              
                axios({
                    method:'get',
                    baseURL:'http://www.liulongbin.top:3005/',
                    params:{
                        
                        pageindex:this.pageIndex,
                        name:this.cmt,
                    },
                    url:`/api/getcomments/${this.$route.params.id}`
                }).then(res=>{
                    console.log(res)
                    if(res.data.status===0){
                        //每当获取新评论的数据时候，不要拔老数据清空覆盖，而是应该以老数据，拼接上新数据
                        this.comments=this.comments.concat(res.data.message)
                    }else{
                        Toast("未获取数据")
                    }
                })
            },
            getMore1() {

                this.pageIndex++
                this.getcmt1()
                
            },
            postComment1(){
                //校验是否为空
               
                if(this.name.trim().length===0){
                    return Toast("评论内容不能为空")
                }
                axios({                                       
                    //发表评论
                    // 参数1：请求的URL地址
                    // 参数2：提交给服务器的数据对象{content：this.name}
                    //参数3：定义提交时候，表单中数据的格式{emultateJSON：true}
                    method:'post',
                    // headers :{ ' content-type ':' application / x-www-form-urlencoded ' },
                    url:`http://www.liulongbin.top:3005/api/postcomment/${this.$route.params.id}`,
                    
                   data: qs.stringify({
                        content:this.name.trim()
                    })
                }).then(res=>{
                    if(res.data.status===0){
                        // console.log(this.$route.params.id)
                        this.cmt={user_name:"匿名用户",add_time:Date.now(),content:this.name.trim()}
                    }else{
                        Toast("添加失败")
                    }
                    this.comments.unshift(this.cmt);
                    this.name=''
                })

            },
        

    },
    props:["id"]
    }
</script>
<style lang="scss" scoped>
    .cmt-comtent{
        h3{
            font-size: 18px;
        }
        textarea{
            font-size: 14px;
            height: 85px;
            margin:0;
        }
        .cmt-list{
            .cmt-item{
                margin: 10px 0;
                .cmt-title{
                    background-color: #cccccc;
                    font-size: 13px;
                   
                }
                .cmt-body{
                    line-height: 35px;
                    text-indent: 1.5em;
                    font-size: 15px;
                }
            }
        }
    }
</style>
